package cn.lanqiao.facelogin.service.impl;

import cn.lanqiao.facelogin.BaiDuFace.FaceAdd;
import cn.lanqiao.facelogin.BaiDuFace.FaceSearch;
import cn.lanqiao.facelogin.BaiDuFace.FaceUpdate;
import cn.lanqiao.facelogin.BaiDuFace.JSONDataReturnValue;
import cn.lanqiao.facelogin.domain.JsonResult;
import cn.lanqiao.facelogin.domain.LoginData;
import cn.lanqiao.facelogin.domain.ResParameter;
import cn.lanqiao.facelogin.domain.Result;
import cn.lanqiao.facelogin.mapper.FaceMapper;
import cn.lanqiao.facelogin.mapper.LoginDataMapper;
import cn.lanqiao.facelogin.service.FaceService;
import cn.lanqiao.facelogin.service.LoginService;
import cn.lanqiao.facelogin.utils.BASE64SaveToLocalImage;
import cn.lanqiao.facelogin.utils.JwtUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Service
public class FaceServiceImpl extends ServiceImpl<FaceMapper, LoginData> implements FaceService {
    @Autowired
    private FaceMapper faceMapper;

    /**
     * 人脸数据更新
     *
     * @param base64
     * @param uuid
     * @return
     * @throws Exception
     */
    @Override
    public Result faceUpdate(String base64, String uuid) throws Exception {
        JsonResult jsonResult = JSONDataReturnValue.m1(FaceSearch.m1(base64));
        if (!jsonResult.isResult()) {
            Result result = JSONDataReturnValue.m2(FaceAdd.m1(base64, uuid));
            if (result.isStatus()) {
                Object faceToken = result.getData();
                UpdateWrapper<LoginData> UpdateWrapper = new UpdateWrapper<>();
                UpdateWrapper.eq("uuid", uuid);
                UpdateWrapper.set("face_token", faceToken);
                int update = faceMapper.update(null, UpdateWrapper);
                if (update == 1) {
                    BASE64SaveToLocalImage.m1(base64, faceToken);
                }
                return new Result(true, ResParameter.LOGIN_SUCCESS_CODE, ResParameter.FACE_ADDED_SUCCESSFULLY, null);
            } else {
                return new Result(false, ResParameter.DENY_THIS_REQUEST, ResParameter.FACE_DETECT_FAIL, null);
            }
        } else if (jsonResult.isResult() && (jsonResult.getJson().equals(uuid))) {
            Result faceToken = JSONDataReturnValue.m2(FaceUpdate.m1(base64, uuid));
            Object json = faceToken.getData();
            String faceTokenOld = faceMapper.selectFaceToken(uuid);
            File file = new File("src/main/resources/static/img/" + faceTokenOld + ".png");
            file.delete();
            UpdateWrapper<LoginData> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("uuid", uuid);
            updateWrapper.set("face_token", json);
            int update = faceMapper.update(null, updateWrapper);
            if (update == 1) {
                BASE64SaveToLocalImage.m1(base64, json);
                return new Result(true, ResParameter.LOGIN_SUCCESS_CODE, ResParameter.FACE_UPDATED_SUCCESSFULLY, null);
            }
        } else {
            Result result = JSONDataReturnValue.m2(FaceAdd.m1(base64, uuid));
            if (result.isStatus()) {
                Object faceToken = result.getData();
                UpdateWrapper<LoginData> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("uuid", uuid);
                updateWrapper.set("face_token", faceToken);
                int update = faceMapper.update(null, updateWrapper);
                if (update == 1) {
                    BASE64SaveToLocalImage.m1(base64, faceToken);
                }
                return new Result(true, ResParameter.LOGIN_SUCCESS_CODE, ResParameter.FACE_ADDED_SUCCESSFULLY, null);
            } else {
                return new Result(false, ResParameter.DENY_THIS_REQUEST, ResParameter.FACE_DETECT_FAIL, null);
            }
        }
        return null;
    }

    @Override
    public String faceTokenObtainTelSaveJwt(String uuid) {
        String tel = faceMapper.selectTel(uuid);
        Map<String, String> map = new HashMap<>();
        map.put(tel,uuid);
        return JwtUtil.getToken(map);
    }
}
